layui.use(['form', 'jquery', 'layer'], function() {
	let form = layui.form;
	let $ = layui.jquery;
	let layer = layui.layer;
	var unchangeable = document.querySelector('.unchangeable');
	var nicknames = document.querySelector('.nickname');
	var emails = document.querySelector('.email');
	var id = document.querySelector('.id');
	//获取父亲页面的元素
	var username = window.parent.document.querySelector('.username');
	form.verify({
		pass: [
			/^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'
		],
		nickname: function(value) {
			if (value.length < 1 || value.length > 6) {
				return '标题必须是1-6个字符';
			}
		},
		email: [
			/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/, '邮箱格式不正确'
		]
	});
	//获取到token
	var token = localStorage.getItem('token');
	//给修改绑定点击事件  
	form.on('submit(registers)', function(data1) {
		ajax({
			type: 'post',
			url: 'http://api-breakingnews-web.itheima.net/my/userinfo',
			token: token,
			data: data1.field,
			success: function(res) {
				if (res.status == 0) {
					username.innerHTML=data1.field.nickname;
					layer.msg(res.message);
				} else {
					layer.msg(res.message);
				}
			}
		})
		return false;
	});
	addInput();
	//添加数据到页面  
	function addInput() {
		ajax({
			type: 'get',
			url: 'http://api-breakingnews-web.itheima.net/my/userinfo',
			token: token,
			success: function(res) {
				unchangeable.value = res.data.username;
				emails.value = res.data.email;
				nicknames.value = res.data.nickname;
				id.value = res.data.id;
			}
		})
	}

});
